#include "gQuadrant.h"

void gQuadrant::init(float a_radius, float a_space)
{
	m_space  = a_space;
	m_radius = a_radius * QUAD_RAD;

	int r = ((int)a_radius) & 0x0000ffff;

	m_starAmt   = (int)((a_radius/4)*(QUAD_RAD*0.1f));
	m_planetAmt = (int)((a_radius/4)*(QUAD_RAD*0.1f));

	genValidPos();
}

void gQuadrant::release()
{
	m_starPos.release();
	m_starRad.release();
	m_planetPos.release();
	m_planetRad.release();
}

void gQuadrant::genValidPos()
{
	//generate valid stars and planets position with spacing
	int amt = m_starAmt+m_planetAmt;

	m_starPos.clear();
	m_planetPos.clear();

	for(int i = 0; i < amt; i++)
	{
		float ang = randF(0,360);
		float rad = ((i < m_starAmt)
			?randF(STAR_MIN,STAR_MAX)
			:randF(PLNT_MIN,PLNT_MAX))
			*2.0f+(m_space*2.0f);
		float dis = randF(0,(m_radius*2.0f)-rad-(m_space*2.0f));
		V2DF  pos = V2DF(dis*cos(RADIAN(ang)),dis*sin(RADIAN(ang)));
		
		bool valid = true;
		for(int j = 0; j < m_starPos.size(); j++)
		{
			if(C2COverlap(pos,rad,m_starPos[j],m_starRad[j]*2.0f+(m_space*2.0f)))
			{
				valid = false;
				break;
			}
		}
		for(int j = 0; j < m_planetPos.size(); j++)
		{
			if(C2COverlap(pos,rad,m_planetPos[j],m_planetRad[j]*2.0f+(m_space*2.0f)))
			{
				valid = false;
				break;
			}
		}

		if(valid)
		{
			if(i < m_starAmt)
			{
				m_starPos.insertBack(pos);
				m_starRad.insertBack(rad/2.0f);
			}
			else
			{
				m_planetPos.insertBack(pos);
				m_planetRad.insertBack(rad/2.0f);
			}
		}
		else
			i--;
	}
}